<?php
namespace app\index\relevan;
use app\index\controller\Common;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\CossModel;
use app\index\model\DeptModel;
use app\index\model\DossModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginModel;
use app\index\model\HouJieModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\XossModel;
use app\index\service\OptService;
use app\index\service\StuffService;

class EnginRelevan extends Common
{
    protected $guiname_dea = 'JT';

    /**
     * 创建项目 动态生成相关数据
     * @param $enginid
     * @return bool
     */
    public function vate_engin_add_mingxi($enginid){
        //生成项目默认文件资料
        $fold = [
            '施工组织方案', '施工日志', '技术交底', '设计变更', '会议纪要', '工程联络单', '收发文',
            '材料合格证', '试验资料', '验收资料', '竣工资料',
        ];
        foreach ($fold as $v){
            EnginFoldModel::insert([
                'comid' => $this->userinfo['comid'],
                'pid' => 0,
                'enginid' => $enginid,
                'type' => 0,
                'name' => $v,
                'created_user' => 0,
                'created_at' => date('Y-m-d H:i:s'),
            ]);
        }

        return true;
    }


    /**
     * 更新项目管理组
     * @param $enginid
     * @return bool
     */
    public function vate_engin($minfo){
        $upd = [];
        if($minfo['business'] == 1){
            $upd['man_zhuan'] = $this->cominfo['man_zhuan'];
            $upd['man_zi'] = $this->cominfo['man_zi'];
            $upd['man_cheng'] = $this->cominfo['man_cheng'];
            $upd['man_zhian'] = $this->cominfo['man_zhian'];
        }else{
            $upd['man_zhuan'] = $this->cominfo['man_zhuan_lian'];
            $upd['man_zi'] = $this->cominfo['man_zi_lian'];
            $upd['man_cheng'] = $this->cominfo['man_cheng_lian'];
            $upd['man_zhian'] = $this->cominfo['man_zhian_lian'];
        }
        EnginModel::where(['id'=>$minfo['id']])->update($upd);
        return true;
    }



    /**
     * 更新项目竣工日期
     * @param $data
     * @return bool
     */
    public function vate_engin_jundt($data){
        EnginModel::where(['id'=>$data['enginid']])->update(['actualdt'=>$data['actualdt'],'filishdt'=>$data['filishdt']]);
        return true;
    }

    /**
     * 更新项目施工合同金额
     * 同步更新  施工合同的更新后金额   【包含补充合同】 (审核中 + 审核完成)
     * @param $enginid
     * @return bool
     */
    public function vate_engin_shimoney($enginid){
        $shimoney = 0;
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->find();
        if($shiinfo){
            $shimoney += $shiinfo['money'];
            $shimoney += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->sum('money');
            ContractShiModel::where(['id'=>$shiinfo['id']])->update(['shimoney'=>$shimoney]);
        }
        EnginModel::where(['id'=>$enginid])->update(['shimoney'=>$shimoney]);
        return true;
    }



    /**
     * 更新项目成本预算【变更后】
     * 同步更新  成本预算的更新后金额
     * @param $bianid
     * @return bool
     */
    public function vate_engin_budget_list($bianid){
        $bianInfo = EnginBudbianModel::where(['id'=>$bianid])->find();
        if(!$bianInfo){
            return true;
        }

        $budgetInfo = EnginBudgetModel::where(['id'=>$bianInfo['budgetid']])->find();
        if(!$budgetInfo){
            return true;
        }

        $bianlist = EnginBudbianListModel::where(['budid'=>$bianInfo['id']])->select()->toArray();

        foreach ($bianlist as $vv){
            if($vv['listid'] > 0){
                EnginBudgetListModel::where(['id'=>$vv['listid']])->update([
                    'num' => $vv['new_num'],
                    'price' => $vv['new_price'],
                    'money' => $vv['new_money'],
                ]);
            }else{
                EnginBudgetListModel::insert([
                    'budid' => $bianInfo['budgetid'],
                    'subid' => $vv['subid'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }

        $listMoney = EnginBudgetListModel::where(['budid'=>$bianInfo['budgetid']])->sum('money');
        EnginBudgetModel::where(['id'=>$bianInfo['budgetid']])->update(['money'=>$listMoney]);
        return true;
    }



    /**
     * 更新采购合同变更明细 及其 变更后金额
     * @param $bianid
     * @return bool
     */
    public function vate_contract_cai_list($bianid){
        $bianinfo = ContractBianModel::where(['id'=>$bianid])->find();

        $cai_money = ContractCaiModel::where(['id'=>$bianinfo['caiid']])->value('money');
        $bian_money = $cai_money + $bianinfo['bianfee'];
        ContractBianModel::where(['id'=>$bianinfo['id']])->update(['yuanfee'=>$cai_money]);
        ContractCaiModel::where(['id'=>$bianinfo['caiid']])->update(['money'=>$bian_money]);


        $list = ContractBianListModel::where(['bianid'=>$bianinfo['id']])->select()->toArray();
        foreach($list as $vv){
            if($vv['listid'] > 0){
                ContractCaiListModel::where(['id'=>$vv['listid']])->update([
                    'listid' => $vv['subid'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                ]);
            }else{
                ContractCaiListModel::insert([
                    'caiid' => $bianinfo['caiid'],
                    'listid' => $vv['subid'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }
        return true;
    }


    /**
     * 材料入库 生成出库单
     * @param $bianid
     * @return bool
     */
    public function vate_stuff_ru_create_chu($minfo){
        if($minfo['ischu'] > 0){
            $list = StuffRuListModel::where(['ruid'=>$minfo['id']])->select()->toArray();
            foreach ($list as $vvv){
                $ins = [];
                $ins['comid'] = $this->userinfo['comid'];
                $ins['chanid'] = $minfo['chanid'];
                $ins['deptid'] = $minfo['deptid'];
                $ins['enginid'] = $minfo['enginid'];
                $ins['ruid'] = $vvv['id'];
                $ins['number'] = (new StuffService())->create_stuff_chu_number();
                $ins['chudt'] = $minfo['rudt'];
                $ins['baner'] = $minfo['baner_chu'];
                $ins['num'] = $vvv['num'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                StuffChuModel::insert($ins);
            }
        }
        return true;
    }







    public function vate_contract_shi_bumber($minfo){
//        $number = "TQ-";
//        $engin = EnginModel::where(['id'=>$minfo['enginid']])->find();
//        $suoxie = DeptModel::where(['id'=>$engin['gui_unit']])->value('abbname');
//        if(!$suoxie){
//            $suoxie = $this->guiname_dea;
//        }
//
//        $lanshi = (new OptService())->get_base_showname('bid_lanshi', $engin['lanshi']);
//        $lanname = $lanshi == '中标' ? 'GC' : 'GC(W)';
//
//        $num_ym = date("Ym", strtotime($minfo['qiandt']));
//
//        $num_shu = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//            ->where(['num_ym'=>$num_ym])->order('num_shu DESC')->value('num_shu');
//        if($num_shu){
//            $num_shu += 1;
//        }else{
//            $num_shu = 1;
//        }
//        $num_shu_str = str_pad($num_shu, 3, '0', STR_PAD_LEFT);
//
//        $number .= $suoxie."-".$lanname."-".$num_ym."-".$num_shu_str;
//
//
//
//        ContractShiModel::where(['id'=>$minfo['id']])->update([
//            'num_ym' => $num_ym,
//            'num_shu' => $num_shu,
//            'number' => $number,
//        ]);

        EnginModel::where(['id'=>$minfo['enginid']])->update(['number'=>$minfo['number']]);
        return true;
    }
    public function vate_contract_bu_bumber($minfo){
        $shiInfo = ContractShiModel::where(['id'=>$minfo['shiid']])->find();

        $num_shu = ContractBuModel::where(['shiid'=>$minfo['shiid']])->order('num_shu DESC')->value('num_shu');
        if($num_shu){
            $num_shu += 1;
        }else{
            $num_shu = 1;
        }
        $num_shu_str = str_pad($num_shu, 3, '0', STR_PAD_LEFT);

        $number = $shiInfo['number']."-补".$num_shu_str;

        ContractBuModel::where(['id'=>$minfo['id']])->update([
            'num_shu' => $num_shu,
            'number' => $number,
        ]);

        return true;
    }

    public function vate_contract_cai_bumber($minfo){
        $number = "TQ-";
        $engin = EnginModel::where(['id'=>$minfo['enginid']])->find();
        $suoxie = DeptModel::where(['id'=>$engin['gui_unit']])->value('abbname');
        if(!$suoxie){
            $suoxie = $this->guiname_dea;
        }


        $num_ym = date("Y", strtotime($minfo['qiandt']));

        $num_shu = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['num_ym'=>$num_ym])->order('num_shu DESC')->value('num_shu');
        if($num_shu){
            $num_shu += 1;
        }else{
            $num_shu = 1;
        }
        $num_shu_str = str_pad($num_shu, 3, '0', STR_PAD_LEFT);

        $number .= $suoxie."-CL-".$num_ym.$num_shu_str;



        ContractCaiModel::where(['id'=>$minfo['id']])->update([
            'num_ym' => $num_ym,
            'num_shu' => $num_shu,
            'number' => $number,
        ]);

        return true;
    }

    public function vate_hou_jie_bumber($minfo){
        $number = "TQ-JK-";

        $num_ym = date("Y", strtotime($minfo['qiandt']));

        $num_shu = HouJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['num_ym'=>$num_ym])->order('num_shu DESC')->value('num_shu');
        if($num_shu){
            $num_shu += 1;
        }else{
            $num_shu = 1;
        }
        $num_shu_str = str_pad($num_shu, 3, '0', STR_PAD_LEFT);

        $number .= $num_ym.$num_shu_str;



        HouJieModel::where(['id'=>$minfo['id']])->update([
            'num_ym' => $num_ym,
            'num_shu' => $num_shu,
            'number' => $number,
        ]);

        return true;
    }





    public function vate_doss_number($id){
        $data = DossModel::where(['id'=>$id])->find();

        $shinumber = '';
        if($data['enginid'] > 0){
            $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$data['enginid']])->find();
            if($shiinfo){
                $shinumber = $shiinfo['number'];
            }
        }


        if($shinumber){
            $num_shu = DossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$data['enginid']])
                ->where('number','like','%'.$shinumber.'%')->order('num_shu DESC')->value('num_shu');
            if($num_shu){
                $num_shu += 1;
            }else{
                $num_shu = 1;
            }
            $num_shu_str = str_pad($num_shu, 3, '0', STR_PAD_LEFT);
            $number = $shinumber."-".$num_shu_str;
            DossModel::where(['id'=>$data['id']])->update([
                'num_shu' => $num_shu,
                'number' => $number,
            ]);
        }else{
            $number = "TQGC-";
            $num_y = date("Y", strtotime($data['guidt']));

            $num_shu = DossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['num_y'=>$num_y])->order('num_shu DESC')->value('num_shu');
            if($num_shu){
                $num_shu += 1;
            }else{
                $num_shu = 1;
            }
            $num_shu_str = str_pad($num_shu, 6, '0', STR_PAD_LEFT);
            $number .= $num_y."-".$num_shu_str;

            DossModel::where(['id'=>$data['id']])->update([
                'num_y' => $num_y,
                'num_shu' => $num_shu,
                'number' => $number,
            ]);
        }


        return true;
    }
    public function vate_coss_number($id){
        $data = CossModel::where(['id'=>$id])->find();
        $number = "TQCS-";
        $num_y = date("Y", strtotime($data['guidt']));

        $num_shu = CossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['num_y'=>$num_y])->order('num_shu DESC')->value('num_shu');
        if($num_shu){
            $num_shu += 1;
        }else{
            $num_shu = 1;
        }
        $num_shu_str = str_pad($num_shu, 6, '0', STR_PAD_LEFT);
        $number .= $num_y."-".$num_shu_str;

        CossModel::where(['id'=>$data['id']])->update([
            'num_y' => $num_y,
            'num_shu' => $num_shu,
            'number' => $number,
        ]);

        return true;
    }
    public function vate_xoss_number($id){
        $data = XossModel::where(['id'=>$id])->find();
        $number = "TQXZ-";
        $num_y = date("Y", strtotime($data['guidt']));

        $num_shu = XossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['num_y'=>$num_y])->order('num_shu DESC')->value('num_shu');
        if($num_shu){
            $num_shu += 1;
        }else{
            $num_shu = 1;
        }
        $num_shu_str = str_pad($num_shu, 6, '0', STR_PAD_LEFT);
        $number .= $num_y."-".$num_shu_str;

        XossModel::where(['id'=>$data['id']])->update([
            'num_y' => $num_y,
            'num_shu' => $num_shu,
            'number' => $number,
        ]);

        return true;
    }







}
